import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Game {
    public static final String[] suits = {"♥","♠","♦","♣"};

    /**
     * 创建一副有序的扑克牌
     * @return
     */
    public List<Card> createCards() {
        List<Card> cardList = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <= 13; j++) {
                Card card = new Card(suits[i],j);
                cardList.add(card);
            }
        }
        return cardList;
    }

    /**
     * 洗牌
     * @param cardList
     */
    public void shuffle(List<Card> cardList) {
        Random random = new Random();
        for (int i = 51; i > 0; i--) {
            int randIndex = random.nextInt(i);
            swap(cardList,i,randIndex);
        }
    }
    //随机交换
    private void swap(List<Card> cardList,int i,int j) {
        Card tmp = cardList.get(j);
        cardList.set(j,cardList.get(i));
        cardList.set(i,tmp);
    }

    /**
     * 发牌，三个人轮流取牌，一次取一张，最终每个人五张牌
     */
    public List<List<Card>> play(List<Card> cardList) {
//        List<Card> cardList1 = new ArrayList<>();
//        List<Card> cardList2 = new ArrayList<>();
//        List<Card> cardList3 = new ArrayList<>();
//        for (int i = 0; i < 5; i++) {
//                cardList1.add(cardList.get(i));
//                cardList.remove(i);
//                cardList2.add(cardList.get(i));
//                cardList.remove(i);
//                cardList3.add(cardList.get(i));
//                cardList.remove(i);
//        }
//        List<List<Card>> eachCardList = new ArrayList<>();
//        eachCardList.add(cardList1);
//        eachCardList.add(cardList2);
//        eachCardList.add(cardList3);
        List<List<Card>> eachCardList = new ArrayList<>();
        List<Card> cardList1 = new ArrayList<>();
        List<Card> cardList2 = new ArrayList<>();
        List<Card> cardList3 = new ArrayList<>();
        eachCardList.add(cardList1);
        eachCardList.add(cardList2);
        eachCardList.add(cardList3);
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                eachCardList.get(j).add(cardList.get(0));
                cardList.remove(0);
            }
        }
        return eachCardList;
    }
}
